<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );


class PimModelProduct extends JModel {
	
	function __construct() {
		parent::__construct();
	}
	
	function getData(&$total, &$dbrows , $order_by = 'id', $order_dir = 'DESC', $offset = 0, $limit = 30, $where='') {
		if ($where) {
			$query = "SELECT * FROM #__product p $where ORDER BY $order_by $order_dir LIMIT $limit" ;
			$this->_db->setQuery($query);
			$dbrows = $this->_db->loadObjectList();
			
			if (count($dbrows[0]) > 0) { //update status of product to used after searching succeed
				$data = $dbrows[0];
				$this->updateStatus($data->id, ManagerHelper::PRODUCT_STATUS_USED_TEXT);
			}
		} else {
			$dbrows = array();
		}		
	}
	
	function updateStatus($id, $status) {
		$query = "UPDATE #__product ";
		$query.= "SET status = '" . $status . "' ";
		$query.= "WHERE id = " . $id;
		
		$this->_db->setQuery($query);
		$this->_db->query();
	}
	
	function getConfig() {
		$query = "SELECT * FROM #__configpim" ;
		$this->_db->setQuery($query);
		$array_config = array();
		$dbrows = $this->_db->loadObjectList();
		
		foreach ($dbrows as $key=>$value) {
			$array_config[trim($value->pim_key)] = $value->pim_value;
		}
		
		return $array_config;  
	}
}